

import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.FileOutputStream; 
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.*; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
import org.apache.commons.fileupload.servlet.ServletFileUpload;  
import org.apache.commons.fileupload.util.Streams; 
import org.apache.commons.io.FilenameUtils;

/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
	
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	File tmpDir = null;
	File saveDir = null;
    public UploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try{  
	        if(ServletFileUpload.isMultipartContent(request)){
	          DiskFileItemFactory dff = new DiskFileItemFactory();  
	          dff.setRepository(tmpDir);  
	          dff.setSizeThreshold(1024000); 
	          ServletFileUpload sfu = new ServletFileUpload(dff);  
	          sfu.setFileSizeMax(5000000); //filesize
	          sfu.setSizeMax(10000000);  //totalsize
	          List<FileItem> items = sfu.parseRequest(request);
	          File record = new File("F:\\COMS E6998\\project\\uploaded\\record.txt");
	          FileWriter fw = new FileWriter(record,true);
	          PrintWriter pw = new PrintWriter(fw);
	          String command = "";
	          for(FileItem item : items) {
	        	  if(item.isFormField() && item.getFieldName().equals("command")) {
	        		  command = item.getString();
	        		  pw.println(command);
	        		  System.out.println(command);
	        	  } else {
	        		  String filename = FilenameUtils.getName(item.getName());
	        		  if(filename.equals("")) {
	        			  pw.println(filename);
	        		  } else {
	        			  InputStream filecontent = item.getInputStream();
		        		  System.out.println(filename);
		        		  pw.println(filename);
			              BufferedInputStream in = new BufferedInputStream(filecontent);
			              BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+"\\"+filename)));  
			              Streams.copy(in, out, true); 
	        		  }	        		  
	        	  }
	          }
	          pw.close();
	          response.getWriter().println("File upload successfully!!!");
	        }  
	    }catch(Exception e){  
	        e.printStackTrace();  
	    }  
	}

	public void init() throws ServletException {  
	    /* Initialize 
	    *  
	    */  
	    super.init();  
	      String tmpPath = "f:\\COMS E6998\\project\\tmp";  
	      String savePath = "f:\\COMS E6998\\project\\uploaded";  
	    tmpDir = new File(tmpPath);  
	    saveDir = new File(savePath);  
	    if(!tmpDir.isDirectory())  
	        tmpDir.mkdir();  
	    if(!saveDir.isDirectory())  
	        saveDir.mkdir();      
	  }
}
